      SUBROUTINE RDCAT3A(TARGDATA,MAXPARM,TARGPARM,KTARGID,TARGNAME,
     *     KTARGTYP,LUERR,IERR)
      IMPLICIT REAL*8 (A-H,O-Z)
C
C THIS IS A SERVICE ROUTINE FOR THE TARGET CATALOGUE READER. IT
C DOES HOUSEKEEPING AND CALLS OTHER ROUTINES TO LOAD TARGET INFO INTO
C OUTPUT ARRAYS.
C
C***********************************************************************
C
C BY C PETRUZZO, GSFC/742, 12/84, 3/85, 7/85.
C    MODIFIED.....
C
C***********************************************************************
C
      INCLUDE 'RDCAT.INC'    ! SUPPLIES MAXDATA VALUE
C
      REAL*8 TARGPARM(MAXPARM),TARGDATA(MAXDATA)
      CHARACTER*16 TARGNAME
      LOGICAL SPECIAL
C
C INITIALIZE
C
      IERR = 0
C
C SEE IF THIS TARGET TYPE REQUIRES SPECIAL HANDLING TO LOAD TARGPARM.
C
      SPECIAL = .FALSE.
      DO I=1,NSPECIAL
        SPECIAL = SPECIAL .OR. KTARGTYP.EQ.KSPECIAL(I)
        END DO
C
C *****************
C *  ERROR CHECK  *
C *****************
C
C SEE IF THERE IS ENOUGH SPACE IN THE TARGPARM ARRAY FOR ALL OF THE
C DATA PRESENT IN THE CATALOGUE RECORD, WHETHER THAT DATA IS REQUIRED
C TO BE THERE OR OTHERWISE.
C
      NITEMS = 0                   ! = NUMBER OF DATA ITEMS PRESENT
      DO I=1,MAXDATA
        IF(TARGDATA(I).NE.DEFALT) NITEMS = I
        END DO
C
      IF(NITEMS.GT.MAXPARM) THEN   ! INSUFFICIENT SPACE ALLOTTED.
        IER1 = KERRFLG2
        GO TO 9900
        END IF
C
C *****************************
C *  LOAD THE TARGPARM ARRAY  *
C *****************************
C
      IF(.NOT.SPECIAL) THEN
        CALL RDCAT3A1(TARGDATA,MAXPARM,TARGPARM,KTARGID,TARGNAME,
     *     KTARGTYP,NLOADED,LUERR,IER1)
      ELSE
        CALL RDCAT3A2(TARGDATA,MAXPARM,TARGPARM,KTARGID,TARGNAME,
     *     KTARGTYP,NLOADED,LUERR,IER1)
        END IF
C
C ***********
C * WRAP UP *
C ***********
C
 9900 CONTINUE
C
C LOAD THE REMAINDER OF TARGPARM WITH VALUES READ FROM THE
C CATALOGUE. IF TARGDATA(I) = DEFALT, LEAVE TARGPARM(I) UNTOUCHED.
C
      IF(IER1.EQ.0) THEN
        KTEMP = MIN(MAXPARM,MAXDATA)
        IF(NLOADED.LT.KTEMP) THEN
          DO I=NLOADED+1,KTEMP
            IF(TARGDATA(I).NE.DEFALT) TARGPARM(I)=TARGDATA(I)
            END DO
          END IF
        END IF
C
C ERROR CONDITION. CATALOGUE RECORD IS MISSING SOME REQUIRED DATA.
C
      IF(IER1.EQ.KERRFLG1) THEN
        IERR = KERRFLG1
        IF(LUERR.GT.0) WRITE(LUERR,1002) KTARGID,TARGNAME
C
C ERROR CONDITION. OUTPUT ARRAY NEEDS MORE ELEMENTS THAN CALLING
C PROGRAM HAS AVAILABLE.
C
      ELSE IF(IER1.EQ.KERRFLG2) THEN
        IERR = KERRFLG2
        IF(LUERR.GT.0)
     *      WRITE(LUERR,1001) KTARGID,TARGNAME,NITEMS,MAXPARM
C
C A MISCELLANEOUS ERROR
C
      ELSE IF(IER1.NE.0) THEN
        IERR = 1
        END IF
C
C
      RETURN
 1001 FORMAT(
     *    ' RDCAT3. TARGET CATALOGUE READER ERROR.'/,
     *    '    CALLING PROGRAM GAVE INSUFFICIENT ',
     *           'SPACE FOR TARGET INFO.'/,
     *    '    TARGET ID=',I5,' TARGET NAME= ',A/,
     *    '    NUMBER OF ELEMENTS NEEDED=',I3,
     *           '   NUMBER AVAILABLE=',I3/,
     *    '    DATA FOR THIS TARGET WAS NOT LOADED.')
 1002 FORMAT(
     *    ' RDCAT3. TARGET CATALOGUE READER ERROR.'/,
     *    '    TARGET CATALOGUE RECORD IS MISSING SOME REQUIRED DATA.'/,
     *    '    TARGET ID=',I5,' TARGET NAME= ',A/,
     *    '    DATA FOR THIS TARGET WAS NOT LOADED.')
      END
